﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess.Exceptions;
using Model;

namespace DataAccess
{
    public static class UserDataAccess
    {
        public static string ConnString = Conexion.DataSource;

        public static User UserExists(string username, string password)
        {
            SqlConnection co = null;
            try
            {
                co = new SqlConnection(ConnString);
                SqlCommand cmd = co.CreateCommand();
                cmd.CommandText = "SELECT * FROM [USER] Where username=@username and password=@password";
                cmd.Parameters.AddWithValue("username", username);
                cmd.Parameters.AddWithValue("password", password);
                co.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                User user = new User();
                while (reader.Read())
                {
                    user.Ci = Convert.ToInt32(reader["ci"].ToString());
                    user.Username = username;
                    user.Password = password;
                    user.Tipo = reader["tipo"].ToString();
                }
                return user;
            }
            catch (SqlException ex)
            {
                throw new DataAccessException("Error getting user", ex);
            }
            finally
            {
                if (co != null)
                {
                    co.Close();
                }
                SqlConnection.ClearAllPools();
            }
        }
        public static User GetUserByUsername(string username)
        {
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(ConnString);
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT * FROM [USER] Where username = @username";
                cmd.Parameters.AddWithValue("username", username);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                User user = new User();

                while (reader.Read())
                {
                    user.Ci = Convert.ToInt32(reader["ci"].ToString());
                    user.Username = username;
                    user.Password = reader["password"].ToString();
                    user.Tipo = reader["tipo"].ToString();
                    
                }
                return user;
            }
            catch (SqlException ex)
            {
                throw new DataAccessException("Error getting an user", ex);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                SqlConnection.ClearAllPools();
            }
        }
    }
}
